package com.atguigu.gulimall.search.thread;

import java.util.concurrent.*;

public class ThreadTest {


    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        System.out.println("main --- start...");
        /**
         * 1）、继承 Thread
         * 2）、实现 Runnable 接口
         * 3）、实现 Callable 接口 + FutureTask （可以拿到返回结果，可以处理异常）
         * 4）、线程池
         */

//        Thread01 thread01 = new Thread01();
//        thread01.start();
//
//        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5,
//                20,
//                10,
//                TimeUnit.SECONDS,
//                new LinkedBlockingDeque<>(50),
//                Executors.defaultThreadFactory(),
//                new ThreadPoolExecutor.AbortPolicy());
//
//
//        Executors.newScheduledThreadPool(1);


//        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//        }, executor);

        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
            return i;
        }, executor).whenComplete((res,exception)->{
            System.out.println("结果：" + res + ";异常："+ exception);
        }).exceptionally(throwable->{
            return 10;
        });
        Integer i = future.get();
        System.out.println("i="+i);


        System.out.println("main --- end...");
    }


    public static class Thread01 extends Thread{

        @Override
        public void run() {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10/2;
            System.out.println("运行结果：" + i);
        }
    }



}
